package com.example.safeassistan.train.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.safeassistan.R;
import com.example.safeassistan.base.BaseVPFragment;
import com.example.safeassistan.train.adapter.TestOptionAdapter;
import com.example.safeassistan.train.model.OptionModel;
import com.example.safeassistan.train.model.SimulateAnswerProblemModel;
import com.example.safeassistan.utile.ToastUtil;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;


public class ProblemSimulateJudgeFragment extends BaseVPFragment {

    private View view;
    private Context context;
    private TextView tvType,tvTitle;
    private RecyclerView rcView;
    private List<OptionModel>  options;
    private TextView tvOkOption,tvUserOption,tvResult;
    private LinearLayout llResult;
    private TestOptionAdapter optionAdapter;
    private int posi=0;//题目标号
    private int positions=0;//在每个分组里面的位置
    LocalBroadcastManager mLocalBroadcastManager;
    private SimulateAnswerProblemModel.ResultDTO.JudgeListDTO data;
    private  boolean isSelected=false;//是否已经选过了
    private String result="3";//1 正确  2 错误 3 未答题
    public static ProblemSimulateJudgeFragment newInstance(int posi, int positions, SimulateAnswerProblemModel.ResultDTO.JudgeListDTO data) {
        Bundle arguments = new Bundle();
        arguments.putInt("posi",posi);
        arguments.putInt("positions",positions);
        arguments.putSerializable("data",(Serializable) data);
        ProblemSimulateJudgeFragment fl = new ProblemSimulateJudgeFragment();
        fl.setArguments(arguments);
        return fl;
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.fragment_similate_answer_problem, container, false);
            context = getActivity();
            initView();
        }
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    @Override
    protected void onFragmentVisibleChange(boolean isVisible) { }

    @Override
    protected void onFragmentFirstVisible(){
    }

    public void initView(){
        mLocalBroadcastManager = LocalBroadcastManager.getInstance(getActivity());
        posi=getArguments().getInt("posi",1);
        positions=getArguments().getInt("positions",0);
        data=(SimulateAnswerProblemModel.ResultDTO.JudgeListDTO)getArguments().getSerializable("data");
        rcView=view.findViewById(R.id.rcView);
        tvTitle=view.findViewById(R.id.tvTitle);
        tvType=view.findViewById(R.id.tvType);
        llResult=view.findViewById(R.id.llResult);
        tvResult=view.findViewById(R.id.tvResult);
        tvOkOption=view.findViewById(R.id.tvOkOption);
        tvUserOption=view.findViewById(R.id.tvUserOption);
        tvType.setText("判断题");
        tvTitle.setText(posi+"."+data.getQuestionsName());
        options=new ArrayList<>();
        String [] option=data.getAnswerContent().split(";");

        for(int i=0;i<option.length;i++){
            String [] s=option[i].split(":");
            options.add(new OptionModel(false,s[1]));
        }

         optionAdapter=new TestOptionAdapter(options);
        rcView.setLayoutManager(new LinearLayoutManager(getContext()));
        rcView.setAdapter(optionAdapter);
        optionAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                //选项的点击事件
                if(isSelected){
                    //已经有选中的  说明已经判断出对错了 不能再改变答案了
                    ToastUtil toastUtil =new ToastUtil(context,R.layout.popu_success,"您已经选择完毕，不可重新选择",1);
                    toastUtil.show();
                    return;
                }
                isSelected=true;
                options.get(position).setSelected(true);
                optionAdapter.notifyDataSetChanged();

                //在这里判断对错
                if (checkOption(position).equals(data.getAnswer())){
                    //选对了
                    tvResult.setText("回答正确");
                    result="1";
                    tvResult.setBackgroundResource(R.drawable.rightcorner_change_blue_bg);
                    tvUserOption.setTextColor(context.getResources().getColor(R.color.blue_3B5BEA));
                }else {
                    //选错了
                    result="2";
                    tvResult.setText("回答错误");
                    tvResult.setBackgroundResource(R.drawable.rightcorner_change_orange_bg);
                    tvUserOption.setTextColor(context.getResources().getColor(R.color.orange));
                }
                llResult.setVisibility(View.VISIBLE);
                tvOkOption.setText(data.getAnswer());
                tvUserOption.setText(checkOption(position));
                Intent intent = new Intent("com.example.safeassistan.ActivitySimulateAnswerProblem");
                intent.putExtra("type",3);
                intent.putExtra("posi",positions);
                intent.putExtra("result",result);
                intent.putExtra("option",checkOption(position));
                mLocalBroadcastManager.sendBroadcast(intent);
            }
        });
    }

    public String checkOption(int posi){
        String option="";
        switch (posi){
            case 0:
                option="A";
                break;
            case 1:
                option="B";
                break;
            case 2:
                option="C";
                break;
            case 3:
                option="D";
                break;
            case 4:
                option="E";
                break;
            case 5:
                option="F";
                break;
            case 6:
                option="G";
                break;
        }
        return option;
    }
}
